<?php
namespace Hilaoyu\ComputeCloudClient\ctyun;

use Hilaoyu\Utils\UtilArr;
use Hilaoyu\Utils\UtilHttp;

class CtyunClientIms extends CtyunClient
{
    protected function baseUrl():string{
        return "https://ctimage-global.ctapi.ctyun.cn";
    }

    public function listImages(int $imageVisibilityCode=1,string $flavorName="",string $imageName="",string $regionId='',$azName='')
    {
        $regionId = $this->getRegionId($regionId);
        $pageNo=1;
        $query = [
            "regionID" => $regionId,
            "imageVisibilityCode" => $imageVisibilityCode,
            "pageSize" => 50,
        ];
        if($flavorName){
            $query['flavorName'] = $flavorName;
        }
        if($imageName){
            $query['queryContent'] = $imageName;
        }
        if($azName){
            $query['azName'] = $azName;
        }

        $images = [];
        $totalPage = 999;
        do{
            $query["pageNo"] = $pageNo;
            $result =  $this->request("/v4/image/list",[],$query,"get");
            if(!$result){
                return;
            }
            $images = array_merge($images,(array)UtilArr::get($result, 'returnObj.images'));
            $totalPage = UtilArr::get($result, 'returnObj.totalPage');
            if($pageNo >= $totalPage){
                break;
            }
            $pageNo++;
        }while(true);

        return $images;
    }
    public function describeImage(string $imageId,string $regionId='')
    {
        $regionId = $this->getRegionId($regionId);
        $query = [
            "regionID" => $regionId,
            "imageID" => $imageId,
        ];

        $result =  $this->request("/v4/image/detail",[],$query,"get");
        if(!$result){
            return;
        }
        return UtilArr::first((array)UtilArr::get($result, 'returnObj.images'));
    }

}
